#!/usr/bin/python3
# -*- encoding:UTF-8 -*-
import time
import serial
import binascii

def trans(c):
    lis = []
    tran = ''
    for word in c.split():
        if len(word) == 1:
            word = '0' + word
            lis.append(word)
        else:
            lis.append(word)
    for element in lis:
        tran = tran + element + ' '
    tran = tran[:-1]
    return tran
            
c = 'ff ff 03 59 6d ae 74 00'

def test():
    print("start")
    # ser = serial.Serial('/dev/ttyAMA0', 115200)
    ser = serial.Serial('/dev/ttyS0', 115200)
    if ser.isOpen == False:
        ser.open()                # 打开串口
    #hex_str = bytes.fromhex(trans(c))
    # hex_str = bytes.fromhex('ff')
    #ser.write(hex_str)
    #print("finish")
    try:
        while True:
            size = ser.inWaiting()               # 获得缓冲区字符
            if size != 0:
                response = ser.read(size)# 读取内容并显示
                #response2 = bytes2Hex(response)
                response = binascii.b2a_hex(response).decode("utf-8")
                print(response)
                if response == '4f':
                    print(response)
                time.sleep(0.1)                  # 软件延时
    except KeyboardInterrupt:
        ser.close()

def main():
    test()

if __name__ == '__main__':
    main()
